package com.example.springbott.wx_service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketGoodsMapper;
import com.example.springbott.mapper.MarketTopicMapper;
import com.example.springbott.vo.WxTopicDetail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/22 14:28
 */
@Service
public class WxTopicServiceIml  implements WxTopicService {
    @Resource
    MarketTopicMapper marketTopicMapper;

    @Resource
    MarketGoodsMapper marketGoodsMapper;

    /**
     * 显示专题
     * @param page
     * @param limit
     * @return
     */
    @Override
    public CommonData<MarketTopic> list(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        List<MarketTopic> list = marketTopicMapper.selectByExample(topicExample);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketTopic> list1 = pageInfo.getList();
        //将查询到的数据进行封装
        CommonData<MarketTopic> data = new CommonData<MarketTopic>((int) total,pages, limit1,page1, list1);
        return data;
    }

    /**
     * 展示专题细节，详情
     * @param id
     * @return
     */
    @Override
    public WxTopicDetail detail(Integer id) {
        MarketTopic data = marketTopicMapper.selectByPrimaryKey(id);
        ArrayList<MarketGoods> goods1 = new ArrayList<>();
        Integer[] goods = data.getGoods();
        for (Integer good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            goods1.add(marketGoods);
        }
        WxTopicDetail wxTopicDetail = new WxTopicDetail();
        wxTopicDetail.setGoods(goods1);
        wxTopicDetail.setTopic(data);
        return wxTopicDetail;
    }

    /**
     * 同样用于展示
     * @param id
     * @return
     */
    @Override
    public CommonData<MarketTopic> related(Integer id) {
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        if (!(id==null||" ".equals(id))) {
            criteria.andIdNotEqualTo(id);
        }
        List<MarketTopic> list = marketTopicMapper.selectByExample(topicExample);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        int limit = pageInfo.getPageSize();
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        int page = pageInfo.getPageNum();
        List<MarketTopic> list1 = pageInfo.getList();
        CommonData<MarketTopic> data = new CommonData<MarketTopic>((int) total, pages,limit,page,list1);
        return data;
    }
}
